package com.sky.task;

import com.sky.entity.BusinessData;
import com.sky.mapper.BusinessDataMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 营业数据定时任务
 */
@Component
public class BusinessDataTask {
    @Autowired
    private WorkSpaceService workSpaceService;
    @Autowired
    private BusinessDataMapper businessDataMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 每天23:30自动查询当天的营业数据,保存到表中
     */
    @Scheduled(cron = "0 30 23 * * ?")
    public void saveBusinessData(){
        BusinessDataVO businessDataVO = workSpaceService.businessData();
        LocalDate today = LocalDate.now();
        //查询当天的订单总数
        Integer orderCount = ordersMapper.countByStatusToday(null,today);
        BusinessData businessData = new BusinessData();
        BeanUtils.copyProperties(businessDataVO,businessData);
        businessData.setToday(today);
        businessData.setOrderCount(orderCount);
        //保存数据
        businessDataMapper.saveData(businessData);
    }
}
